﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RoketPack.Management.Objects;
using System.IO;

namespace RoketPack
{
    class PathTranslator
    {
        private static readonly IDictionary<string, string> m_StaticVariables = new Dictionary<string, string>
            {
                { "system", Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) },
                { "user", Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Applications") },
                { "startmenu", Environment.GetFolderPath(Environment.SpecialFolder.Programs) },
                { "desktop", Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) }
            };

        internal static string Parse(string input)
        {
            return PathTranslator.Parse(input, null);
        }

        internal static string Parse(string input, List<Variable> vars)
        {
            return PathTranslator.Parse(input, vars, false);
        }

        internal static string Parse(string input, List<Variable> vars, bool allowsystem)
        {
            string text = input;
            foreach (KeyValuePair<string, string> kv in PathTranslator.m_StaticVariables)
                if (text.Contains("{" + kv.Key + "}"))
                    text = text.Replace("{" + kv.Key + "}", kv.Value);
            if (vars != null)
                foreach (Variable v in vars)
                    if (text.Contains("{" + v.Name + "}"))
                        text = text.Replace("{" + v.Name + "}", v.Value);

            // Recursively parse the variables.
            if (text == input)
                return text;
            else
                return PathTranslator.Parse(text, vars);
        }
    }
}
